using UnityEngine;
using UnityEditor;
using System.IO;

public class RenderToPNG{

    [MenuItem("Tools/RenderToPNG &p")]
    public static void RenderPNG(){
        Camera cam = SceneView.lastActiveSceneView.camera;
        var rt = new RenderTexture(3840, 2160, 32);
        cam.targetTexture = rt;
        cam.Render();
        
        RenderTexture.active = rt;
        var tex = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, false);
        tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        tex.Apply();
        byte[] bytes = tex.EncodeToPNG();
        File.WriteAllBytes(@"E:\tmp\RenderToPNG.png", bytes);

        RenderTexture.active = null;
        rt.Release();
        Texture2D.DestroyImmediate(tex);
    }


}